# import sys
# import platform

# if platform.system()=='Windows':
#     sys.path.append('d:\\pythonworkspace\\st\\nextt')


import talib
from rqalpha.apis import *

# 在这个方法中编写任何的初始化逻辑。context对象将会在你的算法策略的任何方法之间做传递。
def init(context):
    context.s1 = context.config.params.universe

    # 设置这个策略当中会用到的参数，在策略中可以随时调用，这个策略使用长短均线，我们在这里设定长线和短线的区间，在调试寻找最佳区间的时候只需要在这里进行数值改动
    # context.SHORTPERIOD = 3
    context.LONGPERIOD = 20
    # windows
    context.window = context.LONGPERIOD + 1

    # context.empty = True
    # context.signal_list = []


# 你选择的证券的数据更新将会触发此段逻辑，例如日或分钟历史数据切片或者是实时数据切片更新
def handle_bar(context, bar_dict): # 1.9秒
    # 开始编写你的主要的算法逻辑
    # bar_dict[order_book_id] 可以拿到某个证券的bar信息
    # context.portfolio 可以拿到现在的投资组合状态信息

    # 因为策略需要用到均线，所以需要读取历史数据
    prices = history_bars(context.s1, context.window, '1d', 'close', adjust_type="pre") # 2.2秒

    # 使用talib计算长短两根均线，均线以array的格式表达
    # short_avg = talib.SMA(prices, context.SHORTPERIOD)
    long_avg = talib.SMA(prices, context.LONGPERIOD) # 0.05秒

    # 计算现在portfolio中股票的仓位
    cur_position = get_position(context.s1).quantity # 0.1秒

    close = bar_dict[context.s1].close # 0.1秒
    # 如果短均线从上往下跌破长均线，也就是在目前的bar短线平均值低于长线平均值，而上一个bar的短线平均值高于长线平均值
    if close - long_avg[-1] < 0 and cur_position > 0:
        # 进行清仓
        # context.signal_list.append({'code':context.s1, 'price':close, 'type':'Sell'})
        # context.empty = True
        order_target_value(context.s1, 0) # 0.1秒
        pass

    # 如果短均线从下往上突破长均线，为入场信号
    if close - long_avg[-1] > 0 and cur_position < 100:
        # 满仓入股
        # context.signal_list.append({'code':context.s1, 'price':close, 'type':'Buy'})
        # context.empty = False
        order_target_percent(context.s1, 1) # 2.2秒
        # shares = context.portfolio.cash*0.95 / close
        # order_shares(context.s1, shares, price=close)
        # order_target_value(context.s1, context.portfolio.cash*0.95, price=close)
        pass


config = {
  "params": {
    "universe": "300408.XSHE"
  },
  "base": {
    "start_date": "2012-05-31",
    "end_date": "2022-05-31",
    "accounts": {
        "stock": 1000000
    }
  },
  "extra": {
    "log_level": "error",
  },
  "mod": {
    "sys_analyser": {
      "benchmark": "000300.XSHG",
      "enabled": True, # 1.4秒
      "plot": False
    },
    "sys_progress": {
      "enabled": False,
      "show": False
    },
    "sys_risk": {
      "enabled": True,
      "validate_price": True
    },
    "sys_simulation": {
      "enabled": True,
      "signal": False
    },
    "sys_transaction_cost": {
      "enabled": True
    }
  }
}


if __name__ == '__main__':
    # 您可以指定您要传递的参数
    import time
    import json
    # from rqalpha import run_func
    # import cProfile
    # pr = cProfile.Profile()
    # pr.enable()
    # start = time.time()
    # ret = run_func(init=init, handle_bar=handle_bar, config=config)
    # print(time.time() - start, ret)
    # pr.disable()
    # pr.print_stats('cumtime')
    # 批量回测
    from nextt.tasks import group_file_task

    code_list = \
    ['000001.XSHE', '000002.XSHE', '000063.XSHE', '000066.XSHE', '000069.XSHE', '000100.XSHE', '000157.XSHE', '000166.XSHE', '000301.XSHE', '000333.XSHE', '000338.XSHE', '000408.XSHE', '000425.XSHE', '000538.XSHE', '000568.XSHE', 
    '000596.XSHE', '000625.XSHE', '000651.XSHE', '000661.XSHE', '000703.XSHE', '000708.XSHE', '000725.XSHE', '000768.XSHE', '000776.XSHE', '000786.XSHE', '000792.XSHE', '000800.XSHE', '000858.XSHE', '000876.XSHE', '000877.XSHE', 
    '000895.XSHE', '000938.XSHE', '000963.XSHE', '000977.XSHE', '001289.XSHE', '001979.XSHE', '002001.XSHE', '002007.XSHE', '002008.XSHE', '002027.XSHE', '002032.XSHE', '002049.XSHE', '002050.XSHE', '002064.XSHE', '002074.XSHE', 
    '002120.XSHE', '002129.XSHE', '002142.XSHE', '002179.XSHE', '002202.XSHE', '002230.XSHE', '002236.XSHE', '002241.XSHE', '002252.XSHE', '002271.XSHE', '002304.XSHE', '002311.XSHE', '002352.XSHE', '002371.XSHE', '002410.XSHE', 
    '002414.XSHE', '002415.XSHE', '002459.XSHE', '002460.XSHE', '002466.XSHE', '002475.XSHE', '002493.XSHE', '002555.XSHE', '002568.XSHE', '002594.XSHE', '002600.XSHE', '002601.XSHE', '002602.XSHE', '002607.XSHE', '002648.XSHE', 
    '002709.XSHE', '002714.XSHE', '002736.XSHE', '002791.XSHE', '002812.XSHE', '002821.XSHE', '002841.XSHE', '002916.XSHE', '002920.XSHE', '002938.XSHE', '003816.XSHE', '300003.XSHE', '300014.XSHE', '300015.XSHE', '300033.XSHE', 
    '300059.XSHE', '300122.XSHE', '300124.XSHE', '300142.XSHE', '300207.XSHE', '300223.XSHE', '300274.XSHE', '300316.XSHE', '300347.XSHE', '300408.XSHE', '300413.XSHE', '300433.XSHE', '300450.XSHE', '300454.XSHE', '300496.XSHE', 
    '300498.XSHE', '300529.XSHE', '300595.XSHE', '300601.XSHE', '300628.XSHE', '300661.XSHE', '300750.XSHE', '300751.XSHE', '300759.XSHE', '300760.XSHE', '300763.XSHE', '300782.XSHE', '300866.XSHE', '300896.XSHE', '300919.XSHE', 
    '300957.XSHE', '300979.XSHE', '300999.XSHE', '600000.XSHG', '600009.XSHG', '600010.XSHG', '600011.XSHG', '600015.XSHG', '600016.XSHG', '600018.XSHG', '600019.XSHG', '600025.XSHG', '600028.XSHG', '600029.XSHG', '600030.XSHG', 
    '600031.XSHG', '600036.XSHG', '600048.XSHG', '600050.XSHG', '600061.XSHG', '600085.XSHG', '600089.XSHG', '600104.XSHG', '600111.XSHG', '600115.XSHG', '600132.XSHG', '600150.XSHG', '600161.XSHG', '600176.XSHG', '600183.XSHG', 
    '600188.XSHG', '600196.XSHG', '600219.XSHG', '600276.XSHG', '600309.XSHG', '600332.XSHG', '600346.XSHG', '600352.XSHG', '600362.XSHG', '600383.XSHG', '600406.XSHG', '600426.XSHG', '600436.XSHG', '600438.XSHG', '600460.XSHG', 
    '600489.XSHG', '600519.XSHG', '600547.XSHG', '600570.XSHG', '600584.XSHG', '600585.XSHG', '600588.XSHG', '600600.XSHG', '600606.XSHG', '600655.XSHG', '600660.XSHG', '600690.XSHG', '600741.XSHG', '600745.XSHG', '600760.XSHG', 
    '600763.XSHG', '600795.XSHG', '600809.XSHG', '600837.XSHG', '600845.XSHG', '600886.XSHG', '600887.XSHG', '600893.XSHG', '600900.XSHG', '600905.XSHG', '600918.XSHG', '600919.XSHG', '600926.XSHG', '600941.XSHG', '600958.XSHG', 
    '600989.XSHG', '600999.XSHG', '601006.XSHG', '601009.XSHG', '601012.XSHG', '601021.XSHG', '601066.XSHG', '601088.XSHG', '601100.XSHG', '601111.XSHG', '601117.XSHG', '601138.XSHG', '601155.XSHG', '601166.XSHG', '601169.XSHG', 
    '601186.XSHG', '601211.XSHG', '601216.XSHG', '601225.XSHG', '601229.XSHG', '601236.XSHG', '601238.XSHG', '601288.XSHG', '601318.XSHG', '601319.XSHG', '601328.XSHG', '601336.XSHG', '601360.XSHG', '601377.XSHG', '601390.XSHG', 
    '601398.XSHG', '601600.XSHG', '601601.XSHG', '601618.XSHG', '601628.XSHG', '601633.XSHG', '601658.XSHG', '601668.XSHG', '601669.XSHG', '601688.XSHG', '601698.XSHG', '601728.XSHG', '601766.XSHG', '601788.XSHG', '601799.XSHG', 
    '601800.XSHG', '601808.XSHG', '601816.XSHG', '601818.XSHG', '601825.XSHG', '601838.XSHG', '601857.XSHG', '601865.XSHG', '601868.XSHG', '601877.XSHG', '601878.XSHG', '601881.XSHG', '601888.XSHG', '601898.XSHG', '601899.XSHG', 
    '601901.XSHG', '601916.XSHG', '601919.XSHG', '601939.XSHG', '601966.XSHG', '601985.XSHG', '601988.XSHG', '601989.XSHG', '601995.XSHG', '601998.XSHG', '603019.XSHG', '603087.XSHG', '603160.XSHG', '603185.XSHG', '603195.XSHG', 
    '603259.XSHG', '603260.XSHG', '603288.XSHG', '603290.XSHG', '603369.XSHG', '603392.XSHG', '603486.XSHG', '603501.XSHG', '603659.XSHG', '603799.XSHG', '603806.XSHG', '603833.XSHG', '603882.XSHG', '603899.XSHG', '603986.XSHG', 
    '603993.XSHG', '605499.XSHG', '688005.XSHG', '688008.XSHG', '688012.XSHG', '688036.XSHG', '688065.XSHG', '688111.XSHG', '688126.XSHG', '688169.XSHG', '688363.XSHG', '688396.XSHG', '688561.XSHG', '688599.XSHG', '688981.XSHG']
    batch_params = []
    for c in code_list:
      batch_params.append({"universe": c})
    start = time.time()
    ret = group_file_task(__file__, batch_params, config)
    print(time.time() - start)
    print(json.dumps(ret, indent=2))